﻿using Aspose.Cells;
using LeHouse.Server.Dto;
using LeHouse.Server.Service;
using LeHouse.Server.WebApi.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;

namespace LeHouse.Server.WebApi.Controllers
{
    public class excelController : ApiController
    {

        /// <summary>
        /// excel表格数据导入-估价计算单
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Examination/excelChannel")]
        public dynamic excelChannel([FromBody]uploadingFileHelp obj)
        {
            
            jsonManager jm = new jsonManager();

            byte[] data = Convert.FromBase64String(obj.data.Split(',')[1]);
            string path = AppDomain.CurrentDomain.BaseDirectory;//获取当前控制器所在的绝对路径
            if (obj.type == 1)
            {
                obj.name = "Word\\Excel估价计算单\\" + "项目号（" + obj.T_ReportNumber + ")" + obj.name;
            }
            else
            {
                obj.name = "Word\\其它附件\\" + "项目号（" + obj.T_ReportNumber + ")" + obj.name;
            }
            if (!Directory.Exists(path + "Word\\Excel估价计算单"))
            {
                Directory.CreateDirectory(path + "Word\\Excel估价计算单");
            }
            if (!Directory.Exists(path + "Word\\其它附件"))
            {
                Directory.CreateDirectory(path + "Word\\其它附件");
            }
            FileStream fs = new FileStream(string.Format(path+obj.name), FileMode.Create);
            BinaryWriter br = new BinaryWriter(fs);
            br.Write(data, 0, data.Length);
            br.Close();
            fs.Close();
            Collateral_PersonalService collateral_PersonalService = new Collateral_PersonalService();
            Collateral_Personal d1 = collateral_PersonalService.Select(p => p.T_ReportNumber == obj.T_ReportNumber).FirstOrDefault();

            if (obj.type == 1)
            {
                try
                {
                Cells cells;
                Workbook workbook = new Workbook();
                workbook.Open(path + obj.name);
                cells = workbook.Worksheets[0].Cells;
                string T_ValuationDate = DateTime.Now.ToString("yyyy/MM/dd");//估价时点--todo案例表中找不到估价时点在哪
                string T_ValuationPrice = cells[73, 2].StringValue.Trim();//评估单价(元/㎡)
                string T_Builtupareav = cells[74, 2].StringValue.Trim();//建筑面积(㎡)
                string T_Valueofassessment = cells[76, 6].StringValue.Trim();//评估总值(元)
                string T_Mortgageriskrate = cells[77, 2].StringValue.Trim(); //抵押风险率
                string T_MaximumMortgage = cells[79, 6].StringValue.Trim(); //最高抵押额(元)

                d1.T_ValuationDate = Convert.ToDateTime(T_ValuationDate);
                d1.T_Valueofassessment = T_Valueofassessment;
                d1.T_Builtuparea = Convert.ToInt32(T_Builtupareav);
                d1.T_ValuationPrice = T_ValuationPrice;
                d1.T_ValuationCalculation = obj.name;//存储路径
                d1.T_Mortgageriskrate = T_Mortgageriskrate;
                d1.T_MaximumMortgage = T_MaximumMortgage;
                }
                catch
                {
                    jm.code = 0;
                    jm.msg = "请上传正确的Excel估价计算单！";
                    return Json(jm);
                }
             //   d1.T_State = Convert.ToInt32(EnumSelectState.waitAudit); //报价填完说明可以审核了
            }
            else
            {
                d1.T_Survey = obj.name;
            }
            if (collateral_PersonalService.Update(d1))
            {
                jm.code = 1;
                jm.msg = "成功！";
            }
            else
            {
                jm.code = 0;
                jm.msg = "上传附件失败！";
            }

            return Json(jm);
        }


        /// <summary>
        /// excel表格数据导出
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        //[HttpPost]
        //[Route("api/Examination/excelEduce")]
        //public string excelEduce()
        //{
           
        //}


    }
}
